package wiki.hitime.stl.daq.manager;

import wiki.hitime.stl.daq.bean.db.DaqCheckRecord;
import wiki.hitime.stl.daq.bean.db.DaqCheckRule;
import wiki.hitime.stl.daq.enums.ProcessStatusEnum;
import wiki.hitime.stl.daq.mapper.DaqCheckRecordMapper;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangdaoping
 * @create 2019-07-17 14:39
 */
@Service
public class DaqCheckRecordManager {

    @Resource
    private DaqCheckRecordMapper daqCheckRecordMapper;

    /**
     * 添加未处理记录
     *
     * @param checkRule 规则信息
     * @return 记录id
     */
    public long addNotProcessRecord(Long executeRecordId, DaqCheckRule checkRule, Long acquisitionRecordId) {
        DaqCheckRecord checkRecord = DaqCheckRecord.builder()
                .executeRecordId(executeRecordId)
                .acquisitionRecordId(acquisitionRecordId)
                .checkRuleId(checkRule.getId())
                .checkTime(DateTime.now().toDate())
                .processStatus(ProcessStatusEnum.WAITING.getStatus())
                .ctlVersion(checkRule.getCtlVersion())
                .build();
        daqCheckRecordMapper.insertSelective(checkRecord);
        return checkRecord.getId();
    }

    /**
     * 更新记录为处理成功
     *
     * @param recordId 记录编号
     */
    public void updateProcessSucceed(Long recordId, Boolean checkResult, String checkSummary) {
        DaqCheckRecord checkRecord = DaqCheckRecord.builder()
                .id(recordId)
                .checkResult(checkResult)
                .checkSummary(checkSummary)
                .processStatus(ProcessStatusEnum.SUCCEED.getStatus())
                .build();
        daqCheckRecordMapper.updateByPrimaryKeySelective(checkRecord);
    }

    /**
     * 更新记录为处理失败
     *
     * @param recordId 记录编号
     */
    public void updateProcessFailed(Long recordId, String processMsg) {
        processMsg = processMsg.length() > 2000 ? processMsg.substring(0, 2000) : processMsg;
        DaqCheckRecord checkRecord = DaqCheckRecord.builder()
                .id(recordId)
                .processStatus(ProcessStatusEnum.FAILED.getStatus())
                .processMsg(processMsg)
                .build();
        daqCheckRecordMapper.updateByPrimaryKeySelective(checkRecord);
    }

    /**
     * 根据执行记录编号集合获取检查记录集合
     *
     * @param executeRecordIdList
     * @return
     */
    public List<DaqCheckRecord> list(List<Long> executeRecordIdList) {
        if (CollectionUtils.isEmpty(executeRecordIdList)) {
            return new ArrayList<>();
        }
        Example selectExample = new Example(DaqCheckRecord.class, true, true);
        selectExample.createCriteria().andIn("executeRecordId", executeRecordIdList).andEqualTo("isDelete", Boolean.FALSE);
        return daqCheckRecordMapper.selectByExample(selectExample);
    }
}
